// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

// Array accessor classes run-end encoded arrays

#pragma once

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <nebula/array/array_base.h>
#include <nebula/array/data.h>

#include <turbo/utility/status.h>
#include <nebula/types/type.h>
#include <nebula/types/type_fwd.h>
#include <turbo/base/checked_cast.h>
#include <turbo/base/macros.h>

namespace nebula {

    /// \addtogroup run-end-encoded-arrays
    ///
    /// @{

    // ----------------------------------------------------------------------
    // RunEndEncoded

    /// \brief Array type for run-end encoded data
    class TURBO_EXPORT RunEndEncodedArray : public Array {
    private:
        std::shared_ptr<Array> run_ends_array_;
        std::shared_ptr<Array> values_array_;

    public:
        using TypeClass = RunEndEncodedType;

        explicit RunEndEncodedArray(const std::shared_ptr<ArrayData> &data);

        /// \brief Construct a RunEndEncodedArray from all parameters
        ///
        /// The length and offset parameters refer to the dimensions of the logical
        /// array which is the array we would get after expanding all the runs into
        /// repeated values. As such, length can be much greater than the length of
        /// the child run_ends and values arrays.
        RunEndEncodedArray(const std::shared_ptr<DataType> &type, int64_t length,
                           const std::shared_ptr<Array> &run_ends,
                           const std::shared_ptr<Array> &values, int64_t offset = 0);

        /// \brief Construct a RunEndEncodedArray from all parameters
        ///
        /// The length and offset parameters refer to the dimensions of the logical
        /// array which is the array we would get after expanding all the runs into
        /// repeated values. As such, length can be much greater than the length of
        /// the child run_ends and values arrays.
        static turbo::Result<std::shared_ptr<RunEndEncodedArray>> create(
                const std::shared_ptr<DataType> &type, int64_t logical_length,
                const std::shared_ptr<Array> &run_ends, const std::shared_ptr<Array> &values,
                int64_t logical_offset = 0);

        /// \brief Construct a RunEndEncodedArray from values and run ends arrays
        ///
        /// The data type is automatically inferred from the arguments.
        /// The run_ends and values arrays must have the same length.
        static turbo::Result<std::shared_ptr<RunEndEncodedArray>> create(
                int64_t logical_length, const std::shared_ptr<Array> &run_ends,
                const std::shared_ptr<Array> &values, int64_t logical_offset = 0);

    protected:
        void set_data(const std::shared_ptr<ArrayData> &data);

    public:
        /// \brief Returns an array holding the logical indexes of each run-end
        ///
        /// The physical offset to the array is applied.
        const std::shared_ptr<Array> &run_ends() const { return run_ends_array_; }

        /// \brief Returns an array holding the values of each run
        ///
        /// The physical offset to the array is applied.
        const std::shared_ptr<Array> &values() const { return values_array_; }

        /// \brief Returns an array holding the logical indexes of each run end
        ///
        /// If a non-zero logical offset is set, this function allocates a new
        /// array and rewrites all the run end values to be relative to the logical
        /// offset and cuts the end of the array to the logical length.
        turbo::Result<std::shared_ptr<Array>> LogicalRunEnds(MemoryPool *pool) const;

        /// \brief Returns an array holding the values of each run
        ///
        /// If a non-zero logical offset is set, this function allocates a new
        /// array containing only the values within the logical range.
        std::shared_ptr<Array> LogicalValues() const;

        /// \brief Find the physical offset of this REE array
        ///
        /// This function uses binary-search, so it has a O(log N) cost.
        int64_t FindPhysicalOffset() const;

        /// \brief Find the physical length of this REE array
        ///
        /// The physical length of an REE is the number of physical values (and
        /// run-ends) necessary to represent the logical range of values from offset
        /// to length.
        ///
        /// Avoid calling this function if the physical length can be established in
        /// some other way (e.g. when iterating over the runs sequentially until the
        /// end). This function uses binary-search, so it has a O(log N) cost.
        int64_t FindPhysicalLength() const;
    };

    /// @}

}  // namespace nebula
